package cn.kgc.fcm.dsbank.service.impl;

import cn.kgc.fcm.dsbank.condition.CustomerCondition;
import cn.kgc.fcm.dsbank.dto.ResultDTO;
import cn.kgc.fcm.dsbank.entity.Customer;
import cn.kgc.fcm.dsbank.mapper.CustomerMapper;
import cn.kgc.fcm.dsbank.service.CustomerService;
import cn.kgc.fcm.dsbank.utils.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    private final static Logger log = LoggerFactory.getLogger(CustomerServiceImpl.class);

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ResultDTO<List<Customer>> selectCustomerAll(CustomerCondition condition, Pager pager) {

        List<Customer> customers = customerMapper.selectCustomerAll(condition,pager);

        //根据条件查询记录数
        int i = customerMapper.selectCustomerCountByCondition(condition);

        log.info("查询到的记录数为:{}",i);

        pager.setTotalRow(i);

        log.info("分页对象的数据为:{}",pager);

        if(CollectionUtils.isEmpty(customers)){
            return ResultDTO.buildError();
        }

        return ResultDTO.buildSuccess(customers);
    }

    @Override
    public ResultDTO<Customer> selectCustomerByCustomerId(Long customerId) {

        Customer customer = customerMapper.selectCustomerById(customerId);

        if (null == customer){
            return ResultDTO.buildError();
        }

        return ResultDTO.buildSuccess(customer);
    }

    @Override
    public ResultDTO addCustomer(Customer customer) {
        int i = customerMapper.addCustomer(customer);
        
        if (i == 0){
            return ResultDTO.buildError();
        }
        return ResultDTO.buildSuccess();
    }
}
